﻿Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Collections.Specialized

Public Class Balance3

    Dim dt摸版 As New DataTable

    Private Sub InputUpdate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim SQL As String = "select ID,TITLE as 名称,Count as 金额,MEMO AS 备注 from CrashStatus "

        SQL += " order by 名称"
        Dim dc As New DataGridViewTextBoxColumn

        dc = New DataGridViewTextBoxColumn
        dc.Name = "日期"
        dc.DataPropertyName = "日期"
        dc.Tag = "1"
        dc.Visible = False
        dc.HeaderText = "日期"
        OutlookGrid1.Columns.Insert(0, dc)

        Dim dt As DataTable = ct.FillTable(SQL)
        For index As Integer = dt.Rows.Count - 1 To 0 Step -1
            dc = New DataGridViewTextBoxColumn
            dc.Name = dt.Rows(index)("ID").ToString
            dc.DataPropertyName = dt.Rows(index)("ID").ToString
            dc.Tag = "1"
            dc.HeaderText = dt.Rows(index)("名称").ToString
            OutlookGrid1.Columns.Insert(0, dc)
        Next

        Dim dtc As DataColumn

        For index As Integer = 0 To dt.Rows.Count - 1
            dtc = New DataColumn
            dtc.ColumnName = dt.Rows(index)("ID").ToString
            dtc.DataType = GetType(Decimal)
            dtc.DefaultValue = 0
            dtc.ReadOnly = False
            dt摸版.Columns.Add(dtc)
        Next

        dt摸版.Columns.Add("日期")
        Button1_Click(Nothing, Nothing)

    End Sub

#Region "查询上一个月按钮"

    Public Sub LoadData1(ByVal itype As Integer)

        Dim strBegin As String = Me.dt开始时间.Value.Year.ToString("0000") + "-" + Me.dt开始时间.Value.Month.ToString("00") + "-" + Me.dt开始时间.Value.Day.ToString("00")
        Dim strEnd As String = Me.dt结束时间.Value.Year.ToString("0000") + "-" + Me.dt结束时间.Value.Month.ToString("00") + "-" + Me.dt结束时间.Value.Day.ToString("00")

        If strBegin > strEnd Then
            ShowMessageBox("开始日期大于结束日期")
            dt开始时间.Focus()
            Return
        End If

        Dim sb As New System.Text.StringBuilder
        sb.AppendLine(" SELECT CrashStatusBackup.*  ")
        sb.AppendLine(" FROM CrashStatusBackup ")
        sb.AppendLine(" WHERE date(CrashStatusBackup.日期)>='" + strBegin + "'")
        sb.AppendLine(" AND date(CrashStatusBackup.日期)<='" + strEnd + "'")

        Dim dtBalance As DataTable = ct.FillTable(sb.ToString)
        Dim newTable As DataTable = dt摸版.Clone
        newTable.TableName = "收入"

        If dtBalance.Rows.Count >= 0 Then

            Dim Year As Integer = Me.dt开始时间.Value.Year
            Dim endmouth As Integer = 0
            Dim strBegin1 As String
            Dim strSql2 As String

            For Year = dt开始时间.Value.Year To dt结束时间.Value.Year
                Dim 开始月 As Integer = 1
                Dim 结束月 As Integer = 12
                If Year = dt开始时间.Value.Year Then
                    开始月 = dt开始时间.Value.Month
                End If
                If Year = dt结束时间.Value.Year Then
                    结束月 = dt结束时间.Value.Month
                End If
                For endmouth = 开始月 To 结束月
                    Dim 开始日 As Integer = 1
                    Dim 结束日 As Integer = DateTime.DaysInMonth(Year, endmouth)
                    If Year = dt开始时间.Value.Year AndAlso endmouth = dt开始时间.Value.Month Then
                        开始日 = dt开始时间.Value.Day
                    End If
                    If Year = dt结束时间.Value.Year AndAlso endmouth = dt结束时间.Value.Month Then
                        结束日 = dt结束时间.Value.Day
                    End If
                    For day As Integer = 开始日 To 结束日
                        strBegin1 = Year.ToString("0000") + "-" + endmouth.ToString("00") + "-" + day.ToString("00")

                        Dim dr As DataRow = newTable.NewRow
                        dr("日期") = strBegin1
                        strSql2 = "select  * from CrashStatusBackup where date([日期])='" + strBegin1 + "'"
                        Dim dtData As DataTable = ct.FillTable(strSql2)

                        dtData = ct.FillTable(strSql2)
                        If dtData IsNot Nothing AndAlso dtData.Rows.Count > 0 Then
                            For jdex As Integer = 0 To dtData.Rows.Count - 1
                                For kdex As Integer = 0 To newTable.Columns.Count - 1
                                    If newTable.Columns(kdex).ColumnName = dtData.Rows(jdex)("帐户ID").ToString Then
                                        dr(newTable.Columns(kdex).ColumnName) = dtData.Rows(jdex)("金额")
                                    End If
                                Next
                            Next
                            newTable.Rows.Add(dr)
                        End If
                    Next
                Next
            Next
        End If

        Dim dv As DataView = newTable.DefaultView
        dv.Sort = "日期 desc"
        newTable = dv.ToTable

        Me.OutlookGrid1.AutoGenerateColumns = False
        Me.OutlookGrid1.BindData(newTable, "收入")

        For index As Integer = 0 To OutlookGrid1.Rows.Count - 1
            OutlookGrid1.Rows(index).HeaderCell.Value = newTable.Rows(index)("日期")
        Next

    End Sub

    Private Function 转换对象字符串(ByVal ob As Object) As String
        If ob Is DBNull.Value Then
            Return ""
        End If
        Return ob.ToString
    End Function

    Private Function 转换日期字符串(ByVal ob As Object) As String
        Dim dt As Date = Date.Parse(ob.ToString())
        Return dt.ToString("yyyy年MM月dd日")
    End Function

    Private Function 转换日期字符串月(ByVal ob As Object) As String
        Dim dt As Date = Date.Parse(ob.ToString())
        Return dt.ToString("yyyy年MM月")
    End Function
#End Region

#Region "重载设置窗体函数 A-2010-09-02"

    Public Overrides Function 是否显示设置窗体数据行数() As Boolean
        Return False
    End Function
#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strBegin As String = Me.dt开始时间.Value.Year.ToString("0000") + "-" + Me.dt开始时间.Value.Month.ToString("00") + "-" + Me.dt开始时间.Value.Day.ToString("00")
        Dim strEnd As String = Me.dt结束时间.Value.Year.ToString("0000") + "-" + Me.dt结束时间.Value.Month.ToString("00") + "-" + Me.dt结束时间.Value.Day.ToString("00")

        '余额 今天开始
        If dt开始时间.Value < CDate("2011-11-14") Then
            ShowMessageBox("开始日期必须大于2011-11-13")
            dt开始时间.Focus()
            Return
        End If

        If CDate(dt结束时间.Value) > CDate(Now) Then
            ShowMessageBox("结束日期不能大于系统时间")
            dt结束时间.Focus()
            Return
        End If

        If strBegin > strEnd Then
            ShowMessageBox("开始日期大于结束日期")
            dt开始时间.Focus()
            Return
        End If

        LoadData1(0)

    End Sub

    Public Overrides Sub 保存窗体数据到数据库(ByRef dtData As System.Data.DataTable)
        '年
        dtData.Rows.Add(New String() {"开始时间", "", dt开始时间.Value.ToString("yyyy-MM-dd")})
        dtData.Rows.Add(New String() {"结束时间", "", dt结束时间.Value.ToString("yyyy-MM-dd")})

        MyBase.保存窗体数据到数据库(dtData)

    End Sub

    Public Overrides Sub 存取数据库数据到窗体(ByVal dtDbData As System.Data.DataTable)

        If dtDbData.Rows.Count > 0 Then
            '年
            dt开始时间.Value = 查找数据("开始时间", "", Now.ToString("yyyy-MM-dd"), dtDbData)
            dt结束时间.Value = Now

        End If
        MyBase.存取数据库数据到窗体(dtDbData)
    End Sub

    Private Function 金额(ByVal ob As Object) As Decimal
        If ob Is Nothing Then
            Return 0
        End If
        If ob Is DBNull.Value Then
            Return 0
        End If
        Return CDec(ob)
    End Function

    Private Function 删除数据(ByVal datex As Date) As Boolean

        Dim strSql2 As String = "delete from Balance where date([DATEX])='" + datex + "'"

        ct.ExecuteSql(strSql2)

        Return True

    End Function

    Public Overrides Function DoInsert() As Boolean
        MyBase.DoInsert()
        Using frm As New Balance3Add
            frm.iType = Me.iType
            frm.strName = Me.strName
            frm.ShowDialog()
            If frm.bAdd Then
                LoadData1(0)
            End If
        End Using
        Return True
    End Function

    Public Overrides Function DoDelete() As Boolean

        CrashStatus_Backup.CrashStatus_BackUp_手动_删除(ct, OutlookGrid1.SelectedRows(0).HeaderCell.Value)
        ShowMessageBox("删除成功")

        LoadData1(0)

        Return True

    End Function

#Region "删除CHECK"
    Public Overrides Function DoDeleteCheck() As Boolean

        DoDeleteCheck = False

        If Me.OutlookGrid1.SelectedRows.Count = 0 Then
            ShowMessageBox("没有选中任何记录")
            Exit Function
        End If

        If ShowMessageResult("是否删除本条记录") = Windows.Forms.DialogResult.No Then Exit Function

        DoDeleteCheck = True

    End Function
#End Region

End Class
